'''
多层感知机
'''
from sklearn.neural_network import MLPClassifier
import scipy.io as scio
import datetime
import numpy as np
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.metrics import confusion_matrix

# 读取mat文件
def readMat(matPath):
    return scio.loadmat(matPath)

# 加载数据集
matPath='dataSet20180403-K100.mat'
dataSet=readMat(matPath)
print('数据集读取完成')

# 读取数据集
test=np.array(dataSet['test'])
testX=np.array(dataSet['testX'])
testY=np.array(dataSet['testY']).T[0]
train=np.array(dataSet['train'])
trainX=np.array(dataSet['trainX'])
trainY=np.array(dataSet['trainY']).T[0]
print('数据集读取完成')


# trainXTemp=[]
# trainYTemp=[]
# for sampleIndex in range(len(trainX)):
#     if trainY[sampleIndex]<0.5:
#         if np.random.rand()<0.25:
#             trainXTemp.append(trainX[sampleIndex])
#             trainYTemp.append(trainY[sampleIndex])
#     else:
#         trainXTemp.append(trainX[sampleIndex])
#         trainYTemp.append(trainY[sampleIndex])
# trainX=trainXTemp
# trainY=trainYTemp

# print('1层感知机：')
# paraResult=[]
# activationTest=['tanh','relu','logistic']
# hiddenLayerSizes=[5,10,30]
# for act in activationTest:
#     for hls in hiddenLayerSizes:
#         startTime = datetime.datetime.now()
#         print('activation:' + act + ',hiddenLayer:' + str(hls))
#         clf = MLPClassifier(hidden_layer_sizes=hls, activation=act, learning_rate='adaptive', max_iter=200)
#         clf.fit(trainX, trainY)
#         clf.fit(trainX, trainY)
#         score=clf.score(trainX, trainY)
#         print('训练精度：'+str(score))
#         score=clf.score(testX,testY)
#         print('测试精度：'+str(score))
#         predictY=clf.predict(testX)
#         print('召回率：'+str(metrics.recall_score(testY,predictY)))
#         report=classification_report(testY, predictY, target_names=['0','1'])
#         print('分类结果汇报：\n',report)
#         matrix=confusion_matrix(testY,predictY,labels=[0,1])
#         print('混淆矩阵：\n',matrix)
#
#         paraResultItem = {}
#         paraResultItem['activation'] = act
#         paraResultItem['hiddenLayer'] = hls
#         paraResultItem['score'] = score
#         paraResult.append(paraResultItem)
#         endTime = datetime.datetime.now()
#         print(endTime - startTime)
# print('1层感知机完成')
#
# print('2层感知机：')
# paraResult=[]
# activationTest=['tanh','relu','logistic']
# hiddenLayerSizes1=[5,10,30,50,100]
# hiddenLayerSizes2=[5,10,30,50,100]
# for act in activationTest:
#     for hls1 in hiddenLayerSizes1:
#         for hls2 in hiddenLayerSizes2:
#             startTime = datetime.datetime.now()
#             print('activation:' + act + ',hiddenLayer1:' + str(hls1)+',hiddenLayer2:' + str(hls2))
#             clf = MLPClassifier(hidden_layer_sizes=(hls1,hls2), activation=act, learning_rate='adaptive', max_iter=200)
#             clf.fit(trainX, trainY)
#             clf.fit(trainX, trainY)
#             score=clf.score(trainX, trainY)
#             print('训练精度：'+str(score))
#             score=clf.score(testX,testY)
#             print('测试精度：'+str(score))
#             predictY=clf.predict(testX)
#             print('召回率：'+str(metrics.recall_score(testY,predictY)))
#
#             paraResultItem = {}
#             paraResultItem['activation'] = act
#             paraResultItem['hiddenLayer1'] = hls1
#             paraResultItem['hiddenLayer2'] = hls2
#             paraResultItem['score'] = score
#             paraResult.append(paraResultItem)
#             endTime = datetime.datetime.now()
#             print(endTime - startTime)
# print('2层感知机完成')
#
# print('3层感知机：')
# paraResult=[]
# activationTest=['tanh','relu','logistic']
# hiddenLayerSizes1=[5,10,30,50,100]
# hiddenLayerSizes2=[5,10,30,50,100]
# hiddenLayerSizes3=[5,10,30,50,100]
# for act in activationTest:
#     for hls1 in hiddenLayerSizes1:
#         for hls2 in hiddenLayerSizes2:
#             for hls3 in hiddenLayerSizes3:
#                 startTime = datetime.datetime.now()
#                 print('activation:' + act + ',hiddenLayer1:' + str(hls1)+',hiddenLayer2:' + str(hls2),+',hiddenLayer3:' + str(hls3))
#                 clf = MLPClassifier(hidden_layer_sizes=(hls1,hls2,hls3), activation=act, learning_rate='adaptive', max_iter=500)
#                 clf.fit(trainX, trainY)
#                 score=clf.score(trainX, trainY)
#                 print('训练精度：'+str(score))
#                 score=clf.score(testX,testY)
#                 print('测试精度：'+str(score))
#                 predictY=clf.predict(testX)
#                 print('召回率：'+str(metrics.recall_score(testY,predictY)))
#
#                 paraResultItem = {}
#                 paraResultItem['activation'] = act
#                 paraResultItem['hiddenLayer1'] = hls1
#                 paraResultItem['hiddenLayer2'] = hls2
#                 paraResultItem['hiddenLayer3'] = hls3
#                 paraResultItem['score'] = score
#                 paraResult.append(paraResultItem)
#                 endTime = datetime.datetime.now()
#                 print(endTime - startTime)
# print('3层感知机完成')
#
L1=100
L2=50
L3=20


print(L1,L2,L3)
# tol=float('NaN')
clf = MLPClassifier(hidden_layer_sizes=(L1,L2,L3),verbose=True, activation='logistic', learning_rate='adaptive', max_iter=500)
clf.fit(trainX, trainY)
score=clf.score(trainX, trainY)
print('训练精度：'+str(score))
score=clf.score(testX,testY)
print('测试精度：'+str(score))
predictY=clf.predict(testX)
print('召回率：'+str(metrics.recall_score(testY,predictY)))

print('completed')